package com.lzy.cloud.goods.controller;

import com.lzy.cloud.goods.dtos.CarCalendarDto;
import com.lzy.cloud.goods.facade.CarCalendarFacade;
import com.lzy.cloud.goods.facade.OvertimeBillingFacade;
import com.lzy.cloud.goods.request.CarCalendarRequest;
import com.lzy.cloud.goods.request.TimeAndOvertimeBillingRequest;
import com.lzy.cloud.goods.response.CarCalendarResponse;
import com.lzy.cloud.goods.response.TimeAndOvertimeBillingResponse;
import com.lzy.cloud.goods.service.TimeService;
import com.lzy.cloud.goods.service.carRent.CarCalendarService;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/car/calendar")
public class CarCalendarController {

    @Autowired
    CarCalendarService carCalendarService;

    @Autowired
    CarCalendarFacade carCalendarFacade;

    @Autowired
    private OvertimeBillingFacade overtimeBillingFacade;

    @Autowired
    private TimeService timeService;


    /**
     * 获取单个房车的单个档期(一个日期)详情
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/detail",method = RequestMethod.POST)
    public Result detail(@RequestBody CarCalendarRequest request){
        Ensure.that(request).isNotNull("");
        CarCalendarDto carCalendarDto = carCalendarFacade.selectByCarIdAndDate(DozerUtils.map(request, CarCalendarDto.class));
        Ensure.that(carCalendarDto).isNotNull("");
        return new Result(DozerUtils.map(carCalendarDto,CarCalendarResponse.class));
    }

    /**
     * 单个房车单个档期(一个日期)更新或新增
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/update/single",method = RequestMethod.POST)
    public Result updateSingle(@RequestBody(required = false) CarCalendarRequest request){
        Ensure.that(request).isNotNull("");
        Boolean ok = carCalendarFacade.updateByCarIdAndOneDate(DozerUtils.map(request, CarCalendarDto.class));
        Ensure.that(ok).isTrue("");
        return new Result(ok);
    }

    /**
     * 单个房车的档期区间(多个日期)更新
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/update/many",method = RequestMethod.POST)
    public Result updateMany(@RequestBody(required = false) CarCalendarRequest request){
        Ensure.that(request).isNotNull("");
        Boolean ok = carCalendarFacade.updateByCarIdAndDates(DozerUtils.map(request, CarCalendarDto.class));
        Ensure.that(ok).isTrue("");
        return new Result(ok);
    }

    /**
     * 批量设置单个房车档期区间（区间内的区间）
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/update/weekdays",method = RequestMethod.POST)
    public Result updateWeekdays(@RequestBody(required = false) CarCalendarRequest request){
        Ensure.that(request).isNotNull("");
        Boolean ok = carCalendarFacade.updateWeekdays(DozerUtils.map(request, CarCalendarDto.class));
        Ensure.that(ok).isTrue("");
        return new Result(ok);
    }

    /**
     * 获取档期列表
     * @param request
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public Result calendarList(CarCalendarRequest request){
        Ensure.that(request).isNotNull("");
        List<CarCalendarDto> carCalendarDtoList = carCalendarFacade.selectOneMonthList(DozerUtils.map(request,CarCalendarDto.class));
        if(carCalendarDtoList != null && carCalendarDtoList.size() > 0){
            return new Result(DozerUtils.maps(carCalendarDtoList,CarCalendarResponse.class));
        }
        return new Result();
    }

    /**
     * 查询有没有空档期，有就占用
     * @param request
     * @return
     */
    @RequestMapping(value = "/find/occupy",method = RequestMethod.POST)
    public Result findEmptyCalendarAndOccupy(@RequestBody CarCalendarRequest request){
        Ensure.that(request).isNotNull("");
        return new Result(carCalendarFacade.findEmptyCalendarAndOccupy(DozerUtils.map(request,CarCalendarDto.class)));
    }

    /**
     * 释放档期
     * @param request
     * @return
     */
    @RequestMapping(value = "/release",method = RequestMethod.POST)
    public Result releaseCalendar(@RequestBody CarCalendarRequest request){
        Ensure.that(request).isNotNull("");
        return new Result(carCalendarFacade.releaseCalendar(DozerUtils.map(request,CarCalendarDto.class)));
    }

    /**
     * 商品超时计费和清洁时间一起添加
     * @param request
     * @return
     */
    @RequestMapping(value = "/time/add",method = RequestMethod.POST)
    public Result addTimeAndOvertimeBilling(@RequestBody TimeAndOvertimeBillingRequest request){
        Ensure.that(request).isNotNull("");
        Boolean ok = overtimeBillingFacade.addOvertimeBilling(request);
        Ensure.that(ok).isTrue("");
        return new Result(ok);
    }

    /**
     * 商品超时计费和清洁时间一起查询
     * @param request
     * @return
     */
    @RequestMapping(value = "/time/find",method = RequestMethod.GET)
    public Result findTimeAndOvertimeBilling(TimeAndOvertimeBillingRequest request){
        Ensure.that(request).isNotNull("");
        TimeAndOvertimeBillingResponse response = overtimeBillingFacade.findTimeAndOvertimeBilling(request);
        return new Result(response);
    }

    /**
     * 房车租赁下订单 查询超时计费价格
     * @param request
     * @return
     */
    @RequestMapping(value = "/time/find/price",method = RequestMethod.POST)
    public Result findOverTimePrice(@RequestBody TimeAndOvertimeBillingRequest request){
        Ensure.that(request).isNotNull("");
        return new Result(overtimeBillingFacade.findOverTimePrice(request));
    }

}
